import {
	FITTING_COUNT,
	HALF_PI,
	calcDistance,
	calcMid,
	getBisectorNormals,
	getCubicValue,
	getThirdPoint,
} from "./PlotUtil";

export default class GatheringPlace {
	static generate(pnts) {
		let newPnts = [...pnts];

		if (pnts.length === 2) {
			const mid1 = calcMid(pnts[0], pnts[1]);
			const distance = calcDistance(pnts[0], mid1) / 0.9;
			const pnt = getThirdPoint(pnts[0], mid1, HALF_PI, distance, true);

			newPnts = [pnts[0], pnt, pnts[1]];
		}

		const mid2 = calcMid(newPnts[0], newPnts[2]);
		newPnts.push(mid2, newPnts[0], newPnts[1]);

		let normals = [];
		for (let i = 0; i < newPnts.length - 2; i++) {
			const pnt1 = newPnts[i];
			const pnt2 = newPnts[i + 1];
			const pnt3 = newPnts[i + 2];
			const normalPoints = getBisectorNormals(0.4, pnt1, pnt2, pnt3);
			normals.push(...normalPoints);
		}

		const count = normals.length;
		normals = [normals[count - 1]].concat(normals.slice(0, count - 1));

		const coords = [];
		for (let i = 0; i < newPnts.length - 2; i++) {
			const pnt1 = newPnts[i];
			const pnt2 = newPnts[i + 1];

			for (let t = 0; t < FITTING_COUNT; t++) {
				const pnt = getCubicValue(
					t / FITTING_COUNT,
					pnt1,
					normals[i * 2],
					normals[i * 2 + 1],
					pnt2,
				);
				coords.push(pnt);
			}
			coords.push(pnt2);
		}

		// console.log("gathering place coords:", coords);

		return coords;
	}
}
